//
//  NewFeatureViewController.swift
//  weibo1
//
//  Created by nick on 15/9/26.
//  Copyright © 2015年 nick. All rights reserved.
//

import UIKit
import SnapKit

private let WBNewfeatureImageCount = 4
private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    
    init () {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        super.init(collectionViewLayout: layout)
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        
        self.collectionView!.registerClass(NewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }



    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return WBNewfeatureImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewfeatureCell
    
        // Configure the cell
        cell.backgroundColor = UIColor.whiteColor()
        cell.imageIndex = indexPath.item
        return cell
    }
    // 展示完cell的时候调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems()[0]
        if path.item == WBNewfeatureImageCount - 1 {
            (collectionView.cellForItemAtIndexPath(path) as! NewfeatureCell).startBtnAnima()
        }
    }

}


private class NewfeatureCell: UICollectionViewCell {
    var imageIndex: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    // 按钮动画方法
    private func startBtnAnima () {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }
    // 开始体验按钮监听
    @objc private func startBtnClick () {
        print("开始体验")
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBChangeVCNotificationKey, object: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 添加子控件
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        // 布局
        iconView.frame = bounds
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(snp_centerX)
            make.bottom.equalTo(snp_bottom).offset(-120)
        }
        // 按钮监听方法
        startButton.addTarget(self, action: "startBtnClick", forControlEvents: .TouchUpInside)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 懒加载控件
    lazy var iconView: UIImageView = UIImageView()
    lazy var startButton: UIButton = UIButton(title: "开始体验", imageName: "new_feature_finish_button", titleColor: UIColor.darkGrayColor())

}

